package whf.framework.lucene.searcher;

import java.io.IOException;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.SimpleQueryParser;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexOrderSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import whf.framework.lucene.IndexConfig;
import whf.framework.util.Pair;

/**
 * @author king
 * @create 2008-1-23 下午05:08:56
 * 
 */
public class IndexSearcher {
	protected String indexDirectory = IndexConfig.getIndexDirectory();
	protected Analyzer language = IndexConfig.getAnalyzer();

	public Hits booleanSearch(String fieldName, String searchWord) throws IOException, ParseException {
		Directory dir = FSDirectory.getDirectory(indexDirectory, false);
		org.apache.lucene.search.IndexSearcher indexSearcher = new org.apache.lucene.search.IndexSearcher(dir);
		Query query = QueryParser.parse(searchWord, fieldName, language);
		Hits results = indexSearcher.search(query);
		return results;
	}

	//
	public Hits fieldSearch(List<Pair<String, String>> fields) throws Exception {
		Directory dir = FSDirectory.getDirectory(indexDirectory, false);
		org.apache.lucene.search.IndexSearcher indexSearcher = new org.apache.lucene.search.IndexSearcher(dir);
		StringBuilder sb = new StringBuilder();
		for (Pair<String, String> p : fields) {
			if (sb.length() == 0)
				sb.append(" AND ");
			sb.append(p.getFirst()).append(":").append(p.getSecond());
		}
		String searchWords = "1695";
		Query query = QueryParser.parse(searchWords, "id", language);
		Hits results = indexSearcher.search(query);
		return results;
	}

	/**
	 * 匹配符搜索
	 * 
	 * @author king
	 * @create 2008-1-24 下午03:15:57
	 * @throws Exception
	 */
	public void wildcardSearch() throws Exception {
		Directory dir = FSDirectory.getDirectory(indexDirectory, false);
		org.apache.lucene.search.IndexSearcher indexSearcher = new org.apache.lucene.search.IndexSearcher(dir);
		String[] searchWords = { "tex*", "tex?", "?ex*" };
		Query query;
		for (int i = 0; i < searchWords.length; i++) {
			query = new WildcardQuery(new Term("title", searchWords[i]));
			Hits results = indexSearcher.search(query);
			System.out.println(results.length() + "search results for query " + searchWords[i]);
		}
	}

	/**
	 * 模糊搜索
	 * 
	 * @author king
	 * @create 2008-1-24 下午03:16:20
	 * @throws Exception
	 */
	public void fuzzySearch() throws Exception {
		Directory dir = FSDirectory.getDirectory(indexDirectory, false);
		org.apache.lucene.search.IndexSearcher indexSearcher = new org.apache.lucene.search.IndexSearcher(dir);
		String[] searchWords = { "text", "funny" };
		Query query;
		for (int i = 0; i < searchWords.length; i++) {
			query = new FuzzyQuery(new Term("title", searchWords[i]));
			Hits results = indexSearcher.search(query);
			System.out.println(results.length() + "search results for query " + searchWords[i]);
		}
	}

	public void rangeSearch() throws Exception {
		Directory dir = FSDirectory.getDirectory(indexDirectory, false);
		org.apache.lucene.search.IndexSearcher indexSearcher = new org.apache.lucene.search.IndexSearcher(dir);
		Term begin = new Term("birthDay", "20000101");
		Term end = new Term("birthDay", "20060606");
		Query query = new RangeQuery(begin, end, true);
		Hits results = indexSearcher.search(query);
		System.out.println(results.length() + "search results is returned");
	}

	public Hits fullSearch(String keywords) throws Exception {
		Directory dir = FSDirectory.getDirectory(indexDirectory, false);
		Searcher searcher = null;
		int sortType = IndexOrderSearcher.ORDER_BY_DOCID_DESC;
		searcher = new IndexOrderSearcher(indexDirectory, sortType);

		String indexName = "FullIndex";
		SimpleQueryParser queryParser = new SimpleQueryParser(indexName, language);
		Query query = queryParser.parse(keywords);
		Hits hits = searcher.search(query);
		
		return hits;
	}
	
	public Hits fieldSearch2(String fieldName, String value) throws Exception {
		Directory dir = FSDirectory.getDirectory(indexDirectory, false);
		org.apache.lucene.search.IndexSearcher indexSearcher = new org.apache.lucene.search.IndexSearcher(dir);
		Term term = new Term(fieldName, value);
		Query query = new TermQuery(term);
		Hits results = indexSearcher.search(query);
		return results;
	}
}
